#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "../DEMURA/Demura.h"



u8 btn_read_buffer[256];
void NT38350_M_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(SigMode != CMD_Mode)
		 SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
		delay_ms(5);
		SSD2828_W_Array(SigMode,channel,buffer,2);
		delay_ms(50);
		//SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
   // STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           

}

void NT38350_M_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
 	u8 buffer1[7];
	u16 tmp;
	u8 tempReg;
	u8 i,j;
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
	buffer[2]=buffer[4];
	Generate_MIPI_Read(pdev,SigMode,channel,buffer[3] ,buffer[4] , btn_read_buffer);
	for(j=0;j<buffer[2];j++)
	{
			buffer[4+j]=btn_read_buffer[j];	
	}	

  buffer[1]=0x02;
  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
  STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+4);           //返回 读取的 Gamma数据  ：ok  	
}


void Btn_Read_Type1_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 i=0;
	u8 readTotalLength=0;
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
	for( i=0;i<buffer[2]/2;i++)
	{
   	Generate_MIPI_Read(pdev,SigMode,channel,buffer[3+2*i] ,buffer[3+2*i+1], btn_read_buffer+readTotalLength);
		readTotalLength+=buffer[3+2*i+1];
	}
	for(i=0;i<readTotalLength;i++)
	{
		buffer[3+i]=btn_read_buffer[i];
	}
	buffer[2]=readTotalLength;
  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  	
	
}
void Btn_Read_Type0_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u16 tmp;
	u8 tempReg;
	u8 i,j;

	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
	for(j=0;j<buffer[2];j++)
	{
		Generate_MIPI_Read(pdev,SigMode,channel,buffer[3+j] ,1, btn_read_buffer+j);
		//delay_us(10);
	}
	
	for(j=0;j<buffer[2];j++)
	{
			
			buffer[3+j]=btn_read_buffer[j];	

	}	
  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
  STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  	
}

void NT38350_M_delay(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
		
		delay_ms(buffer[3]<<8|buffer[4]);
	  buffer[4] = Uart_Error_None;
    //STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
}


//write  gamma
void Auto_Type1_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[54];
			int j=0,i=0;
			delay_ms(5);	
	    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
      delay_ms(5);
			for(i=0;i<buffer[2];i++)
			{
				for(j=0;j<=((int)buffer[i+3]);j++)
				{
					buffer1[j]=buffer[i+j+3];
					
				}
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				i+=buffer[i+3];
				//delay_ms(50);
			}
				delay_ms(5);
				SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void Auto_Type0_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[3];
			delay_ms(5);	
	    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
      delay_ms(5); 
			buffer1[0]=0x02;
			for( i =1;i<(buffer[2]+1)/2;i++) 
			{	 					
			buffer1[1]=buffer[2*i+1];
			buffer1[2]=buffer[2*i+2];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			}		
				delay_ms(5);
				SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void Normal_BGR_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	USB_Rx_Demura_End_Flag=0;	
	if(USB_Rx_Demura==0)
	{												
		Demura_first_package_flag=0;		
		DemuraTransOnce=OLED.H_pixel*3;
		USB_Rx_Demura=1; 												
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //Ð´Èëµ¥±ÊÊý¾Ý,????DemuraTransOnce???
	{												
 //raydium & viewtrix
		if(Demura_first_package_flag==0)
		{
				Demura_first_package_flag=1;
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
		}
		else
		{
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);	
		}
		
		SPI_CS_Select(channel,0);
		SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
		SPI_CS_Select(channel,1);	
		
		VCP_Receive_True_num=0;
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//
		{
			Demura_START_FLAG=0;  
			DemuraRecivedNum=0; 
			USB_Rx_Demura=0;//
			Demura_first_package_flag=0; //2c/3c
		}
	}
}

void Normal_RGB_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	USB_Rx_Demura_End_Flag=0;	
	if(USB_Rx_Demura==0)
	{												
		Demura_first_package_flag=0;		
		DemuraTransOnce=OLED.H_pixel*3;
		USB_Rx_Demura=1; 												
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //Ð´Èëµ¥±ÊÊý¾Ý,????DemuraTransOnce???
	{												
 //raydium & viewtrix
		if(Demura_first_package_flag==0)
		{
				Demura_first_package_flag=1;
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
		}
		else
		{
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);	
		}
		
		SPI_CS_Select(channel,0);
		SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
		SPI_CS_Select(channel,1);	
		
		VCP_Receive_True_num=0;
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//
		{
			Demura_START_FLAG=0;  
			DemuraRecivedNum=0; 
			USB_Rx_Demura=0;//
			Demura_first_package_flag=0; //2c/3c
		}
	}
}
void BMP_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	USB_Rx_Demura_End_Flag=0;	
	if(USB_Rx_Demura==0)
	{												
		Demura_first_package_flag=0;		
		DemuraTransOnce=OLED.H_pixel*3;
		USB_Rx_Demura=1; 												
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //Ð´Èëµ¥±ÊÊý¾Ý,????DemuraTransOnce???
	{												
 //raydium & viewtrix
		if(Demura_first_package_flag==0)
		{
				Demura_first_package_flag=1;
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
		}
		else
		{
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 					
				//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);	
		}
		
		SPI_CS_Select(channel,0);
		SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
		SPI_CS_Select(channel,1);	
		
		VCP_Receive_True_num=0;
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//
		{
			Demura_START_FLAG=0;  
			DemuraRecivedNum=0; 
			USB_Rx_Demura=0;//
			Demura_first_package_flag=0; //2c/3c
		}
	}
}



void ProcessForIc80( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(buffer[1])
	{
		case 0x01:                                      //写寄存器
			 NT38350_M_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
		case 0x02:                                      //读寄存器
			 NT38350_M_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
		case 0x03:                                      //读寄存器
			 NT38350_M_delay(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;		
		case 0x04:                                      //写Gamma数据    
			Auto_Type0_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x05:                                      //写Gamma数据    
			Auto_Type1_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x06:                                      //读Gamma数据
			 Btn_Read_Type0_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x07:                                      //读Gamma数据
			 Btn_Read_Type1_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x08:                                      //读Gamma数据
			 Normal_RGB_Demura_Ram(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x09:                                      //读Gamma数据
			 Normal_BGR_Demura_Ram(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
	
    default:	break;	
	}	
}
void ProcessForDmuR35( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
				Normal_RGB_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
				Normal_BGR_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
				BMP_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
	
			default:
				break;												
	}
}

